package com.dems.domain;

import com.dems.core.enumerate.ResEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonResponse<T> {

    public int code;

    public boolean success;

    public String message;

    public T data;

    public JsonResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonResponse(T data) {
        this.code = ResEnum.SUCCESS.getCode();
        this.message = ResEnum.SUCCESS.getMsg();
        this.data = data;
    }

    public JsonResponse(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> JsonResponse<T> success() {
        return new JsonResponse<>(ResEnum.SUCCESS.getCode(),
                ResEnum.SUCCESS.getMsg());
    }

    public static <T> JsonResponse<T> success(T data) {
        return new JsonResponse<>(ResEnum.SUCCESS.getCode(),
                ResEnum.SUCCESS.getMsg(), data);
    }

    public static <T> JsonResponse<T> fail() {
        return new JsonResponse<>(ResEnum.PROCESSING.getCode(),
                ResEnum.ERROR.getMsg());
    }

    public static <T> JsonResponse<T> failMsg(String msg) {
        return new JsonResponse<>(ResEnum.ERROR.getCode(), msg);
    }
    public static <T> JsonResponse<T> dupFail() {
        return new JsonResponse<>(ResEnum.DUP_DATA.getCode(),
                ResEnum.ERROR.getMsg());
    }

    public boolean isSuccess() {
        return code == ResEnum.SUCCESS.getCode();
    }

}
